;
(function (call) {
    var main = {
        userInfo: null,
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            var time = new Date().format('yyyy-MM-dd hh:mm');
            $("#year").val(new Date(time).format('yyyy'));
            $("#date").val(new Date(time).format('MM'));
            $("#day").val(new Date(time).format('dd'));
            var afterDay = new Date(time);
            afterDay.setHours(afterDay.getHours() + 48);
            afterDay = afterDay.format("MM月dd日");
            var afterDay2 = new Date(time);
            afterDay2.setHours(afterDay2.getHours() + 72);
            afterDay2 = afterDay2.format("MM月dd日");
            $("#afterDay input").val(afterDay);
            $("#afterDay2 input").val(afterDay2);
            this.getJwbData();
        },
        getJwbData: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var params = {
                "product_id": productInfo.productId,
                "fortime": productInfo.fortime,
                "queryTime": new Date().format('yyyy-MM-dd 08:00:00')
            }
            $.getJSON(api.internal.common.getProductDetail, params, function (res) {
                if (!res.isSuccess) return;
                if (JSON.stringify(res.data) == "{}") {
                    that.initJwbData();
                    that.initJwbData2();
                } else {
                    var data = res.data.txtContent;
                    $("#proText").val(data);
                    var dataList = JSON.parse(res.data.data.content);
                    dataList = dataList.tempData.curTagData
                    $("#tmin3").val(dataList.i_jwb_07.val)
                    $("#tmax2").val(dataList.i_jwb_06.val)
                    $("#tmin1").val(dataList.i_jwb_05.val)
                    $("#afterDay2 input").val(dataList.i_jwb_04.val)
                    $("#afterDay input").val(dataList.i_jwb_03.val)
                    $("#tmin4").val(dataList.i_jwb_09.val)
                    $("#afterDay input").val(dataList.i_time_02.val)
                    $("#tmax3").val(dataList.i_jwb_08.val)
                    $("#afterDay2 input").val(dataList.i_time_03.val)
                    $("#tmax4").val(dataList.i_jwb_10.val)
                    $("#wp4").val(dataList.i_jwb_18.val)
                    $("#wd4").val(dataList.i_jwb_17.val)
                    $("#wp3").val(dataList.i_jwb_16.val)
                    $("#wd3").val(dataList.i_jwb_15.val)
                    $("#wp2").val(dataList.i_jwb_14.val)
                    $("#wd2").val(dataList.i_jwb_13.val)
                    $("#rainchance4").val(dataList.s_04.val.substr(0, dataList.s_04.val.length - 1))
                    $("#wp1").val(dataList.i_jwb_12.val)
                    $("#wd1").val(dataList.i_jwb_11.val)
                    $("#rainchance1").val(dataList.s_01.val.substr(0, dataList.s_01.val.length - 1))
                    $("#rainchance3").val(dataList.s_03.val.substr(0, dataList.s_03.val.length - 1))
                    $("#rainchance2").val(dataList.s_02.val.substr(0, dataList.s_02.val.length - 1))
                    $("#wea4").val(dataList.i_jwb_04.val)
                    $("#wea3").val(dataList.i_jwb_03.val)
                    $("#wea2").val(dataList.i_jwb_02.val)
                    $("#wea1").val(dataList.i_jwb_01.val)
                }
            })
        },
        getParams: function () {
            var params = {
                fcstTime: new Date().format('yyyyMMdd080000'),
                element: 'TP,TMAX,TMIN,EDA,WEA',
                startPeriod: 0,
                endPeriod: 48,
                interval: 12,
                stationType: 0,
                lon: "117.21",
                lat: "39.07"
            }
            return params;
        },
        initJwbData: function () {
            var that = this;
            loading.add('.cafe-container');
            var url = api.external.getGdybDataByParamData
            //url = api.external.getJwbJson
            $.getJSON(url, that.getParams(), function (res) {
                loading.remove();
                if (!res.success || !res.resultData) return;
                if ($.isEmptyObject(res.resultData)) return;
                var resultData = res.resultData['117.21_39.07']
                var wdLi = WindUtil.coverWindDesc(resultData['EDA'][2][1]),
                    wpLi = WindUtil.coverWindDesc(resultData['EDA'][3][1]),
                    wdLi2 = WindUtil.coverWindDesc(resultData['EDA'][2][2]),
                    wpLi2 = WindUtil.coverWindDesc(resultData['EDA'][3][2]);
                var wea1 = weatherUtil.getVal(resultData['WEA'][0][1])
                var wea2 = weatherUtil.getVal(resultData['WEA'][0][2])
                $("#wea1").val(resultData['WEA'][0][1])
                $("#rainchance1").val(wea1.substr(0, wea1.length - 1))
                $("#wd1").val(wdLi)
                $("#wp1").val(wpLi)
                $("#tmin1").val(resultData['TMIN'][0][2] == '9999' ? '--' : Math.round(resultData['TMIN'][0][1]))
                $("#wea2").val(resultData['WEA'][0][2])
                $("#rainchance2").val(wea2.substr(0, wea2.length - 1))
                $("#wd2").val(wdLi2)
                $("#wp2").val(wpLi2)
                $("#tmax2").val(resultData['TMAX'][0][2] == '9999' ? '--' : Math.round(resultData['TMAX'][0][3]))
            })
        },
        initJwbData2: function () {
            var that = this;
            var url = api.external.getGdybDataByParamData
            //url = api.external.getJwbJson
            var params = that.getParams();
            params.startPeriod = 36
            params.endPeriod = 108
            params.interval = 24
            $.getJSON(url, params, function (res) {
                if (!res.success || !res.resultData) return;
				if ($.isEmptyObject(res.resultData)) return;
                var resultData = res.resultData['117.21_39.07']
                var wdLi = WindUtil.coverWindDesc(resultData['EDA'][2][0]),
                    wpLi = resultData['EDA'][3][0].replace('_','转'),
                    wdLi2 = WindUtil.coverWindDesc(resultData['EDA'][2][1]),
                    wpLi2 = resultData['EDA'][3][1].replace('_','转');
                var wea1 = weatherUtil.getVal(resultData['WEA'][0][0])
                var wea2 = weatherUtil.getVal(resultData['WEA'][0][1])
                $("#wea3").val(resultData['WEA'][0][0])
                $("#rainchance3").val(wea1.substr(0, wea1.length - 1))
                $("#wd3").val(wdLi)
                $("#wp3").val(wpLi)
                
                $("#wea4").val(resultData['WEA'][0][1])
                $("#rainchance4").val(wea2.substr(0, wea2.length - 1))
                $("#wd4").val(wdLi2)
                $("#wp4").val(wpLi2)
                
                params.startPeriod = 24
                params.endPeriod = 96
                $.getJSON(url, params, function (res2) {
                    if (!res2.success || !res2.resultData) return;
                    if ($.isEmptyObject(res2.resultData)) return;
                    var resultData2 = res2.resultData['117.21_39.07']
                    $("#tmin3").val(resultData2['TMIN'][0][0] == '9999' ? '--' : Math.round(resultData2['TMIN'][0][0]))
                    $("#tmax3").val(resultData2['TMAX'][0][0] == '9999' ? '--' : Math.round(resultData2['TMAX'][0][1]))
                    
                    $("#tmin4").val(resultData2['TMIN'][0][1] == '9999' ? '--' : Math.round(resultData2['TMIN'][0][1]))
                    $("#tmax4").val(resultData2['TMAX'][0][1] == '9999' ? '--' : Math.round(resultData2['TMAX'][0][2]))
                })
                
            })
        },
        publishJwbData: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var data = {
                "tempData": {
                    "curTagData": {
                        "i_jwb_07": {
                            "val": $("#tmin3").val()
                        },
                        "i_jwb_06": {
                            "val": $("#tmax2").val()
                        },
                        "i_jwb_05": {
                            "val": $("#tmin1").val()
                        },
                        "i_jwb_04": {
                            "val": $("#wea4").val(),
                        },
                        "i_jwb_03": {
                            "val": $("#wea3").val(),
                        },
                        "i_jwb_02": {
                            "val": $("#wea2").val(),
                        },
                        "i_jwb_01": {
                            "val": $("#wea1").val(),
                        },
                        "i_time_01": {
                            "val": new Date().format('yyyyMMdd')
                        },
                        "i_jwb_09": {
                            "val": $("#tmin4").val()
                        },
                        "i_time_02": {
                            "val": $("#afterDay input").val(),
                        },
                        "i_jwb_08": {
                            "val": $("#tmax3").val()
                        },
                        "i_time_03": {
                            "val": $("#afterDay2 input").val(),
                        },
                        "i_jwb_10": {
                            "val": $("#tmax4").val()
                        },
                        "i_jwb_18": {
                            "val": $("#wp4").val()
                        },
                        "i_jwb_17": {
                            "val": $("#wd4").val()
                        },
                        "i_jwb_16": {
                            "val": $("#wp3").val()
                        },
                        "i_jwb_15": {
                            "val": $("#wd3").val()
                        },
                        "i_jwb_14": {
                            "val": $("#wp2").val()
                        },
                        "i_jwb_13": {
                            "val": $("#wd2").val()
                        },
                        "s_04": {
                            "val": $("#rainchance4").val() + '%',
                            "id": "6"
                        },
                        "i_jwb_12": {
                            "val": $("#wp1").val()
                        },
                        "i_jwb_11": {
                            "val": $("#wd1").val()
                        },
                        "s_01": {
                            "val": $("#rainchance1").val() + '%',
                            "id": "0"
                        },
                        "s_03": {
                            "val": $("#rainchance3").val() + '%',
                            "id": "5"
                        },
                        "s_02": {
                            "val": $("#rainchance2").val() + '%',
                            "id": "4"
                        }
                    }
                }
            }
            data = JSON.stringify(data)
            $.ajax({
                url: api.internal.jwb.pushforeJWB,
                type: "get",
                data: {
                    "forecasterId": that.userInfo.smuser.iid,
                    "product_id": productInfo.productId,
                    "fortime": productInfo.fortime,
                    "jsonStr": data,
					"taskId": productInfo.taskId
                },
                dataType: "json",
                success: function (res) {
                    if (res.isSuccess) {
                        alert("发布成功");
                        CafeJS.menuFunc.setTask();
                        that.getJwbData();
                    } else {
                        alert("发布失败")
                    }
                },
            })
        },
        bindEvt: function () {
            var that = this;
            $("#publishBtn").click(function () {
                that.publishJwbData();
            })
            $("#returnBtn").click(function () {
                that.initJwbData();
                that.initJwbData2();
            })
            $(".forecast-list input").change(function () {
                var id = $(this).attr('id'),
                    val = $(this).val();
                if (id.indexOf('wea') > -1) {
                    var rainchance = weatherUtil.getVal(val);
                    rainchance = rainchance.substr(0, rainchance.length - 1)
                    $("#rainchance" + id.charAt(id.length - 1)).val(rainchance);
                }
            })
        }
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
})